XNA 4.0 draw a cube with DrawUserIndexedPrimitives method [on hold]
Posted
by
Leggy7
on Stack Overflow
See other posts from Stack Overflow
or by Leggy7
Published on 2013-10-18T14:31:46Z
Indexed on
2013/10/22
9:55 UTC
Read the original article
Hit count: 118
EDIT
Since I read what Mark H suggested (thanks a lot, I found it very useful) I think my question can become clearer structured this way:
Using XNA 4.0, I'm trying to draw a cube.
Im using this method:
GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
PrimitiveType.LineList,
primitiveList,
0, // vertex buffer offset to add to each element of the index buffer
8, // number of vertices in pointList
lineListIndices, // the index buffer
0, // first index element to read
7 // number of primitives to draw
);
I got the code sample from this page which simply draw a serie of triangles.
I want to modify this code in order to draw a cube. I was able to slitghly move the camera so I can have the perception of solidity, I set the vertex array to contain the 8 points defining a cube. But I can't fully understand how many primitives I have to draw (last parameter) for each of PrimitiveType
.
So, I wasn't able to draw the cube (just some of the edges in a non-defined order).
More in detail:
to build the vertex index list, the sample used
// Initialize an array of indices of type short.
lineListIndices = new short[(points * 2) - 2];
// Populate the array with references to indices in the vertex buffer
for (int i = 0; i < points - 1; i++)
{
lineListIndices[i * 2] = (short)(i);
lineListIndices[(i * 2) + 1] = (short)(i + 1);
}
I'm ashamed to say I cannot do the same in the case of a cube.
- what has to be the size of the
lineListIndices
? - how should I populate it? In which order?
And how do these things change when I use a different PrimitiveType
?
In the code sample there are also another couple of calls which I cannot fully understand, which are:
// Initialize the vertex buffer, allocating memory for each vertex.
vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, vertexDeclaration,
points, BufferUsage.None);
// Set the vertex buffer data to the array of vertices.
vertexBuffer.SetData<VertexPositionColor>(pointList);
and
vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Color, VertexElementUsage.Color, 0)
}
);
that is, for VertexBuffer
and VertexDeclaration
I could not find and significant (monkey-like) guide. I reported them too because I think they could be involded in understanding things.
I think I also have to understand something related to the order the vertexes are stored in the array. But actually I have no clue of what I should learn to have this function drawing a cube. So, if anybody could point me to the right direction, it wil be appreciated.
Hope to have made myself clear this time
© Stack Overflow or respective owner